#include <cstring>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

void test()
{
    ifstream ifs;
    ifs.open("test.cc");

    ifstream ifs2("test.cc");

    string filename = "test.cc";
    ifstream ifs3(filename);

    ifs.close();
    ifs2.close();
    ifs3.close();
}

void test1()
{
    ifstream ifs("test.cc");
    char buff[1024] = {0};

    //when get /n or delim getline will cut the string
    while(ifs.getline(buff, sizeof(buff), '"'))
    {
        cout << buff << endl;
        memset(buff,0,sizeof(buff));
    }
    ifs.close();
}

void test2()
{
    //usually in work
    ifstream ifs("test.cc");
    string line;
    
    while(getline(ifs, line, '"'))
    {
        cout << line << endl;
    }
    ifs.close();
}

void test3()
{
    //read exactly number of stream
    ifstream ifs("test.cc");
    if(!ifs){
        cerr << "ifs open file fail!";
        return;
    }
    string str;

    ifs.seekg(0, std::ios::end);
    long length = ifs.tellg();
    cout << length << endl;

    char *pdata = new char[length + 1]();
    ifs.seekg(0,std::ios::beg);
    ifs.read(pdata,length);

    string contant(pdata);
    cout << "contant:" << contant << endl;

    delete []pdata;
    ifs.close();
}

void test4()
{
    //input into file
    string filename = "test.cc";
    ofstream ofs(filename, std::ios::app);

    //use ostream to input
    string sbuff;
    getline(cin, sbuff);
    ofs << sbuff;

    //use write to input
    char buff[] = "int b;";
    ofs.write(buff, strlen(buff) + 1);

    ofs.close();
}

int main()
{
    test4();
    return 0;
}

